<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view v-if="!addressInfo" class="address-btn u-flex u-row-center">
        <u-button
          shape="circle"
          :plain="true"
          size="medium"
          :hair-line="false"
          :custom-style="customStyle"
          @click="handleChooseAddress"
          >选择地址</u-button
        >
      </view>
      <view v-else class="address-detail">
        <view class="address-detail-item">{{ detail }}</view>
        <view class="address-user"
          >{{ addressInfo.userName }} {{ addressInfo.telNumber }}</view
        >
      </view>
    </view>
    <u-line color="#eee"></u-line>
    <!-- 2 已选商品 -->
    <view class="goods-list u-p-15 u-m-t-10">
      <view class="goods-list-title u-m-l-16 u-m-b-26">已选商品</view>
      <view class="goods-list-content">
        <view
          class="cart-item"
          v-for="item in checkedCartList"
          :key="item.goods_id"
        >
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              :src="item.goods_small_logo"
              width="191rpx"
              height="191rpx"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price"
                ><view class="goods-price-item">￥</view
                >{{ item.goods_price }}</view
              >
              <view class="goods-num-tool">
                <view class="goods-vnum">x {{ item.num }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price">
        合计:
        <text class="text"> <text class="nbm">￥</text>{{ totalPrice }}</text>
      </view>
      <view class="all-count">
        <u-button
          @click="handlePay"
          :custom-style="allCount"
          type="error"
          shape="circle"
          >去支付({{ totalCount }})</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      customStyle: {
        // 注意驼峰命名，并且值必须用引号包括，因为这是对象
        height: "90rpx",
        width: "304rpx",
        border: "4rpx solid #EA4350",
        fontSize: "36rpx",
        fontWeight: "700",
        color: "#EA4350",
      },
      allCount: {
        height: "52rpx",
        fontSize: "21rpx",
      },
      // 收货地址信息
      addressInfo: null,
    };
  },
  computed: {
    ...mapGetters("cart", ["checkedCartList", "totalPrice", "totalCount"]),
    ...mapState("user", ["token"]),
    detail() {
      // cityName: "广州市"
      // countyName: "海珠区"
      // detailInfo: "新港中路397号"
      // errMsg: "chooseAddress:ok"
      // nationalCode: "510000"
      // postalCode: "510000"
      // provinceName: "广东省"
      // telNumber: "020-81167888"
      // userName: "张三"
      if (!this.addressInfo) return "";
      const { provinceName, cityName, countyName, detailInfo } =
        this.addressInfo;
      return provinceName + cityName + countyName + detailInfo;
    },
  },
  methods: {
    ...mapActions("user", ["fetchToken"]),
    async handleChooseAddress() {
      const [err, address] = await uni.chooseAddress();
      if (err) {
        console.log(err);
      } else {
        this.addressInfo = address;
      }
    },
    async handlePay() {
      //判断有没有选择地址
      if (!this.addressInfo) {
        uni.showToast({
          title: "您还没有选择地址哦",
          icon: "none",
        });
        return;
      }

      // 判断有没有token
      if (!this.token) {
        // 获取token  一定要加上 await
        await this.fetchToken();
      }

      //创建订单
      const orderParams = {
        order_price: this.totalCount,
        consignee_addr: this.detail,
        goods: this.checkedCartList.map((item) => ({
          goods_id: item.goods_id,
          goods_number: item.num,
          goods_price: item.goods_price,
        })),
      };

      const res3 = await this.$u.http.post("/my/orders/create", orderParams, );
      const { order_number } = res3;

      const res4 = await this.$u.http.post(
        "/my/orders/req_unifiedorder",
        { order_number },
      );
      const { pay } = res4;

      //开始调起微信支付
      uni.requestPayment(pay);
    },
  },
};
</script>

<style lang="scss">
.address {
  height: 188rpx;
  margin: 20rpx;
  // background-color: green;
  .address-btn {
    height: 100%;
  }
}
.address-detail {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  .address-detail-item {
    // background-color: darkorange;
    color: #333;
    font-size: 30rpx;
  }
  .address-user {
    // background-color: blue;
    font-size: 26rpx;
    color: #434343;
  }
}
.pay {
  padding-bottom: 86rpx;
}
.goods-list-title {
  font-size: 26rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 1;

    display: flex;
    align-items: center;
    // justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
      color: #434343;
      font-size: 24rpx;
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: $theme-color;
        font-size: 24rpx;
        .goods-price-item {
          font-size: 18rpx;
          display: inline-block;
        }
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
          font-size: 28rpx;
          margin-right: 15rpx;
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 84rpx;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #292929;
    font-size: 26rpx;
    margin-left: 10rpx;
    .text {
      margin-left: 10rpx;
      font-size: 28rpx;
      color: $theme-color;
      .nbm {
        font-size: 20rpx;
      }
    }
  }

  //   .all-count {
  //   }
}
</style>